﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Client.Panels
{
    public class GGPanel : BasePanel
    {
        protected override string[] parentsName => new string[] { "BasePanel" };
        public override string panelName => "GGRankingPanel";
        string[] symbols;
        string kindName;

        public GGPanel(string clientText, params string[] symbols):base(clientText)
        {
            this.symbols = symbols;
            this.kindName = "";
            FillFields<DataCenter.Models.StockInfo>(panelName);
            RegisterHotkey(panelName);
        }
        public GGPanel(string kindName,string[] symbols, string clientText):base(clientText)
        {
            this.kindName = kindName;
            this.symbols = symbols;
            RegisterHotkey("GGPanel");
            FillFields<DataCenter.Models.StockInfo>(panelName);
            RegisterHotkey("GGPanel");
        }


        protected override void UpdatePanel()
        {
            UpdatePanel<DataCenter.Models.StockInfo>(Managers.Manager.cronM.ggStocks);
            UpdateHotStockInfo();
        }

        public override void Enter()
        {
            var p = new GGKLinePanel(Managers.Manager.cronM.ggStocks[y],"day");
            Managers.Manager.panelM.AddPanel(p);
        }

        public override void OnAwake()
        {
            base.OnAwake();
            if (symbols[0] == "" && symbols.Length == 1)
            {
                Managers.Manager.ttsM.Speak("此分类下暂无股票信息");
                return;
            }
            Managers.Manager.cronM.UpdateGGSymbol(symbols);
            Managers.Manager.cronM.updateSuccessfully += UpdatePanel;
            Managers.Manager.cronM.StartCron(true);
            Managers.Manager.zxgM.removedZXG += RemovedZXGCallback;
        }

        public override void OnSleep()
        {
            base.OnSleep();
            if (symbols.Length==0 || (symbols[0] == "" && symbols.Length == 1))
                return;
            Managers.Manager.cronM.updateSuccessfully -= UpdatePanel;
            Managers.Manager.zxgM.removedZXG -= RemovedZXGCallback;

            Managers.Manager.hotStockInfo = null;
        }

        protected override void ClearCron()
        {
            Managers.Manager.cronM.ClearGGSymbols();
        }

        public void RemovedZXGCallback(string[] symbols )
        {
            if (symbols.Length != 0)
            {
                Managers.Manager.cronM.UpdateGGSymbol(symbols);
                Managers.Manager.cronM.StartCron(true);
                y--;
            }
            else
            {
                items.Clear();
                InitIndex();
                Managers.Manager.cronM.ClearGGSymbols();
                Managers.Manager.hotStockInfo = null;
                //Managers.Manager.cronM.StartCron(false);
            }
        }

        protected override void UpdateHotStockInfo()
        {
            if (y > -1)
                StockMenuFuncs.UpdateHotStockInfo(Managers.Manager.cronM.ggStocks[y]);
        }
    }
}
